package ${package}.${moduleName}.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ${package}.${moduleName}.po.${ClassName}PO;
import ${package}.${moduleName}.vo.${ClassName}Query;
import ${package}.${moduleName}.vo.${ClassName}VO;
import ${package}.${moduleName}.dao.${ClassName}Dao;
import ${package}.${moduleName}.service.${ClassName}Service;
import com.qm.qfc.common.exception.BusinessException;
import com.qm.qfc.common.table.domain.DbUnionCommonDto;
import com.qm.qfc.common.table.domain.QmPage;
import com.qm.qfc.common.table.service.impl.BaseTableServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * ${tableComment}
 *
 * @author ${author} ${email}
 * @since ${version} ${date}
 */
@Service
@AllArgsConstructor
public class ${ClassName}ServiceImpl extends BaseTableServiceImpl<${ClassName}Dao, ${ClassName}PO> implements ${ClassName}Service {

    @Override
    public List<${ClassName}VO> list${ClassName}(${ClassName}Query ${className}Query) {
        // 构造查询条件
        LambdaQueryWrapper<${ClassName}PO> queryWrapper = getQueryWrapper(${className}Query);

        // 执行查询
        List<${ClassName}PO> ${className}POList = this.list(queryWrapper);

        // 转换成 vo 返回
         return BeanUtil.copyToList(${className}POList, ${ClassName}VO.class);
    }

    @Override
    public Page<${ClassName}VO> listPage${ClassName}(${ClassName}Query ${className}Query) {
        // 构造分页条件
        Page<${ClassName}PO> page = new Page<>(${className}Query.getCurrentPage(), ${className}Query.getPageSize());

        // 构造查询条件
        LambdaQueryWrapper<${ClassName}PO> queryWrapper = getQueryWrapper(${className}Query);

        // 执行查询
        Page<${ClassName}PO> ${className}POPage = this.page(page, queryWrapper);

        // 转换成 vo
        List<${ClassName}VO> ${className}VOList = new ArrayList<>(${className}POPage.getRecords().size());
        ${className}POPage.getRecords().forEach(${className}PO ->
        ${className}VOList.add(BeanUtil.copyProperties(${className}PO, ${ClassName}VO.class)));
        Page<${ClassName}VO> returnPage = new Page<>();
        BeanUtil.copyProperties(${className}POPage, returnPage, "records");
        returnPage.setRecords(${className}VOList);

        return returnPage;
    }

    @Override
    public QmPage<${ClassName}VO> listQmPage${ClassName}(${ClassName}Query ${className}Query) {
        // 构造查询条件，pageSize 设置为-1取消分页
        // ${className}Query.setPageSize(-1);
        DbUnionCommonDto<${ClassName}PO> searchPO = getDbUnionCommonDto(${className}Query);

        // 默认的排序
        if (CharSequenceUtil.isEmpty(${className}Query.getTsortby())) {
        ${className}Query.setTsortby("create_time|descend, last_modified|ascend");
        }

        // 执行查询
        return this.getQueryPage(${className}Query, searchPO);
    }

    @Override
    public void saveBatch${ClassName}(List<${ClassName}VO> ${className}VOList) {
        // vo 转成 po
        List<${ClassName}PO> ${className}POList = BeanUtil.copyToList(${className}VOList, ${ClassName}PO.class);

        // 保存${className}POList
        this.saveBatch(${className}POList);
    }

    @Override
    public void updateBatch${ClassName}(List<${ClassName}VO> ${className}VOList) {
        // vo 转成 po
        List<${ClassName}PO> ${className}POList = BeanUtil.copyToList(${className}VOList, ${ClassName}PO.class);

        // 更新${className}POList
        this.updateBatchById(${className}POList);
    }

    @Override
    public void save${ClassName}(${ClassName}VO ${className}VO) {

        LambdaQueryWrapper<${ClassName}PO> queryWrapper = new LambdaQueryWrapper<>();
        // vo 转成 po
        ${ClassName}PO ${className}PO = BeanUtil.copyProperties(${className}VO, ${ClassName}PO.class);

        // 保存${className}PO
        this.save(${className}PO);
    }

    @Override
    public void update${ClassName}(${ClassName}VO ${className}VO) {

        // vo 转成 po
        ${ClassName}PO ${className}PO = BeanUtil.copyProperties(${className}VO, ${ClassName}PO.class);
    
        // 更新${className}PO
        this.updateById(${className}PO);
        }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete${ClassName}(List<String> idList) {
        super.removeByIds(idList);
    }

    @Override
    public ${ClassName}VO detail(String id) {
        ${ClassName}PO ${className}PO = getById(id);

        return BeanUtil.copyProperties(${className}PO, ${ClassName}VO.class);
    }

    /**
    * 构造查询条件
    *
    * @param query 查询条件
    * @return 查询条件
    */
    private LambdaQueryWrapper<${ClassName}PO> getQueryWrapper(${ClassName}Query query) {
        LambdaQueryWrapper<${ClassName}PO> queryWrapper = new LambdaQueryWrapper<>();
        return queryWrapper;
    }

    /**
    * 构造查询条件
    *
    * @param query 查询条件
    * @return 查询条件
    */
    private DbUnionCommonDto<${ClassName}PO> getDbUnionCommonDto(${ClassName}Query query) {
        DbUnionCommonDto<${ClassName}PO> searchPO = new DbUnionCommonDto<>();
        return searchPO;
    }

}